package com.simtop.beefsheepmaching.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author lvyongwei
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${url.whiteList}")
    private List<String> whiteList;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //允许访问的接口地址
        registry.addMapping("/**")
                //允许发起跨域访问的域名
                .allowedOriginPatterns("*")
                //允许跨域访问的方法
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
                //是否带上cookie信息
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(whiteList);
    }
}
